<?php

namespace common\models\goods;

use Yii;

/**
 * This is the model class for table "{{%goods_type_attr}}".
 *
 * @property integer $id
 * @property integer $type_id
 * @property integer $attr_id
 * @property string $group_name
 * @property integer $sort_order
 */
class GoodsTypeAttr extends \common\models\AR
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%goods_type_attr}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type_id', 'attr_id'], 'required'],
            [['type_id', 'attr_id', 'sort_order'], 'integer'],
            [['group_name'], 'string', 'max' => 45]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'type_id' => '类型ID',
            'attr_id' => '属性ID',
            'group_name' => '分组名称',
            'sort_order' => '属性排序，越大越靠前',
        ];
    }
    
    public function beforeDelete() {
        if (parent::beforeDelete()) {
            $count = GoodsAttrVal::find()->where('attr_id=:attr_id', [':attr_id' => $this->attr_id])->count();
            return $count == 0;
        } else {
            return false;
        }
    }

    /**
     * 获取属性信息
     * @return [[GoodsAttr]] 商品属性类
     */
    public function getAttr(){
        return $this->hasOne(GoodsAttr::className(), ['id' => 'attr_id']);
    }
}
